package com.yiran.dao;

import java.sql.*;
import java.util.*;

import com.yiran.model.SortClass;
import com.yiran.util.DBConnection;

public class SortBean {
	private Connection con;
	SortClass sort = new SortClass();

	public SortBean() {
		try {
			con = DBConnection.getConnection();
			System.out.println("conn: " + con);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setStyleInfo(SortClass sort) {
		this.sort = sort;
	}

	public void addSortInfo() throws Exception {
		try {

			PreparedStatement stm = con
					.prepareStatement("insert into sort(sortName) values(?)");
			stm.setString(1, sort.getName());

			try {
				stm.executeUpdate();
			} catch (Exception e) {
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public Collection getSorts() {
		SortClass sort;
		ArrayList arraylist = new ArrayList();
		try {
			Statement statement = con.createStatement();
			ResultSet resultset = statement.executeQuery("select * from sort");

			while (resultset.next()) {
				sort = new SortClass();
				int id = resultset.getInt(1);
				String name = resultset.getString(2);

				sort.setId(id);
				sort.setName(name);
				arraylist.add(sort);
			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return arraylist;
	}
	
	public void deleteSort(String s) {
		try {
			int i = Integer.parseInt(s);
			Statement statement = con.createStatement();
			statement.execute("delete from sort where id='" + i + "'");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
